package com.tesis_2.DA;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.tesis_2.MO.MOComentario;
import com.tesis_2.MO.MODscore;
import com.tesis_2.MO.MOLocal;

public class DAOComentarios {
	
	public static ArrayList<MOComentario> getComentarioByRestId(Integer restid) throws JSONException {
		//DAORestaurants.lsRest = lsRest;
		
		ArrayList<MOComentario> lsRest = new ArrayList<MOComentario>();
		
	    try{

	    	JSONArray rests = (new JSONReaderArray().execute(DAOUtils.getServer()+ "/commentaries/getrestbyid/"+restid+".json")).get();
	    	
	    	if (rests == null) return lsRest;
	       	    	
	    	for (int i = 0; i < rests.length(); i++) {
	    		JSONObject jsonComent = rests.getJSONObject(i);
	    	    jsonComent = jsonComent.getJSONObject("commentary");
	    	   
	    	    MOComentario objCmt = new MOComentario();
	    	    
	    	    objCmt.setId(jsonComent.getInt("id"));
	    	    objCmt.setCmt(jsonComent.getString("cmt"));
	    	    objCmt.setCreated_at(DAOUtils.parseDateIso(jsonComent.getString("created_at")));
	    	    objCmt.setLocal_id(jsonComent.getInt("local_id"));
	    	    objCmt.setRate((float)jsonComent.getDouble("rate"));
	    	    objCmt.setReservation_id(jsonComent.getInt("reservation_id"));
	    	    objCmt.setRestaurant_id(jsonComent.getInt("restaurant_id"));
	    	    objCmt.setUpdated_at(DAOUtils.parseDateIso(jsonComent.getString("updated_at")));
	    	    objCmt.setUser_id(jsonComent.getInt("user_id"));
	    	    objCmt.setStatus(jsonComent.getInt("status"));
	    	    objCmt.setUsrname(jsonComent.getString("usrname"));
	    	    //[{"commentary":{"cmt":"Esto es un comentario de prueba.","created_at":"2012-12-11T22:52:43Z",
	    	    //"id":1,"local_id":2,"rate":3,"reservation_id":1,"restaurant_id":2,
	    	    //"updated_at":"2012-12-11T22:52:43Z","user_id":1,"usrname":"Hunter"}}]
	    	    lsRest.add(objCmt);
	    	}
	 
	    }catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return lsRest;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return lsRest;
		}
		return lsRest;
		
	}

	public static MOComentario updateComentario(Integer resid ,MOComentario comment) throws JSONException {
		//DAORestaurants.lsRest = lsRest;
		
		MOComentario objComment = new MOComentario();
		
	    try{
	    	
	    	
	    	String url = DAOUtils.getServer()+"/commentaries/updateCommentary/" + resid + 
	    									"/" + comment.getCmt() + "/" + comment.getRateAsString() + "/"
	    									 + comment.getRrest() + "/" + comment.getRlocal() + ".json";
	    	
	    	JSONObject rests = (new JSONReaderObject().execute(url)).get();
	    	
	    	if (rests == null) return objComment;
	       	    	
	    	
	    	JSONObject jsonComent = rests.getJSONObject("commentary");
	    	   
	    	MOComentario objCmt = new MOComentario();
	    	    
	    	objCmt.setId(jsonComent.getInt("id"));
	    	objCmt.setCmt(jsonComent.getString("cmt"));
	        objCmt.setCreated_at(DAOUtils.parseDateIso(jsonComent.getString("created_at")));
	   	    objCmt.setLocal_id(jsonComent.getInt("local_id"));
	   	    objCmt.setRate((float)jsonComent.getDouble("rate"));
	   	    objCmt.setReservation_id(jsonComent.getInt("reservation_id"));
	   	    objCmt.setRestaurant_id(jsonComent.getInt("restaurant_id"));
    	    objCmt.setUpdated_at(DAOUtils.parseDateIso(jsonComent.getString("updated_at")));
	        objCmt.setUser_id(jsonComent.getInt("user_id"));
	   	    objCmt.setStatus(jsonComent.getInt("status"));
	   	    objCmt.setUsrname(jsonComent.getString("usrname"));
	   	    //[{"commentary":{"cmt":"Esto es un comentario de prueba.","created_at":"2012-12-11T22:52:43Z",
	   	    //"id":1,"local_id":2,"rate":3,"reservation_id":1,"restaurant_id":2,
    	    //"updated_at":"2012-12-11T22:52:43Z","user_id":1,"usrname":"Hunter"}}]
	    	    
	    	
	 
	    }catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return objComment;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return objComment;
		}
		
		return objComment;
		
	}
	
	public static ArrayList<MODscore> getDscoresByResId(Integer resid)
	{
		
		ArrayList<MODscore> lsDscores = new ArrayList<MODscore>();
		
	    try{

	    	JSONArray dscores = (new JSONReaderArray().execute(DAOUtils.getServer()+ "/dscores/getDscoresByReservationid/"+ resid +".json")).get();
	    	
	    	if (dscores == null) return lsDscores;
	       	    	
	    	for (int i = 0; i < dscores.length(); i++) {
	    		JSONObject jsondscores = dscores.getJSONObject(i);
	    		jsondscores = jsondscores.getJSONObject("dscore");
	    	    //obtengo el restaurante
	    	    MODscore objDscore = new MODscore();
	    	    //entro al objeto y leo los atributos
	    	    objDscore.setDishid(jsondscores.getInt("dish_id"));
	    	    objDscore.setDishname(jsondscores.getString("dish_name"));
	    	    objDscore.setId(jsondscores.getInt("id"));
	    	    objDscore.setRate(jsondscores.getInt("rate"));
	    	    objDscore.setReservationid(jsondscores.getInt("resid"));
	    	    objDscore.setStatus(1);
	    	    objDscore.setUserid(jsondscores.getInt("user_id"));
	    	    objDscore.setUsername(jsondscores.getString("user_name"));
	    	    //objDscore.setUpdated_at(DAOUtils.parseDateIso(jsondscores.getString("updated_at")));
	    	    //objDscore.setCreated_at(DAOUtils.parseDateIso(jsondscores.getString("created_at")));
	    	    
	    	  //objLocal.setPhoto(jsonlocales.getString("photo_file_name"));
	    	    //{"local":{"addr1":"Address1","addrs2":"Address2","capacity":20,"created_at":"2012-12-10T02:48:59Z",
	    	    //"descr":"Centro de Lima","id":1,"name":"Centro","phone1":"123465","phone2":"78946",
	    	    //"restaurant_id":1,"status":1,"updated_at":"2012-12-10T02:49:22Z"}}
	    	    
	    	    lsDscores.add(objDscore);
	    	}
	 
	    }catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return lsDscores;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return lsDscores;
		}
		return lsDscores;
	}

	public static MODscore updateDscore(Integer dscore_id ,Integer rate) throws JSONException {
		//DAORestaurants.lsRest = lsRest;
		
		MODscore objDscore = new MODscore();
		
	    try{
	    	
	    	
	    	String url = DAOUtils.getServer()+"/dscores/updateDscore/" + dscore_id + 
	    									"/" + rate + ".json";
	    	
	    	JSONObject dscore = (new JSONReaderObject().execute(url)).get();
	    	
	    	if (dscore == null) return objDscore;
	       	    	
	    	
	    	JSONObject jsondscores = dscore.getJSONObject("dscore");
	    	   
	    	//MOComentario objCmt = new MOComentario();
	    	    
	    	 //MODscore objDscore = new MODscore();
	    	 //entro al objeto y leo los atributos
	    	 objDscore.setDishid(jsondscores.getInt("dish_id"));
	    	 objDscore.setDishname(jsondscores.getString("dish_name"));
	    	 objDscore.setId(jsondscores.getInt("id"));
	    	 objDscore.setRate(jsondscores.getInt("rate"));
	    	 objDscore.setReservationid(jsondscores.getInt("resid"));
	    	 objDscore.setStatus(1);
	    	 objDscore.setUserid(jsondscores.getInt("user_id"));
	    	 objDscore.setUsername(jsondscores.getString("user_name"));
	   	    //[{"commentary":{"cmt":"Esto es un comentario de prueba.","created_at":"2012-12-11T22:52:43Z",
	   	    //"id":1,"local_id":2,"rate":3,"reservation_id":1,"restaurant_id":2,
    	    //"updated_at":"2012-12-11T22:52:43Z","user_id":1,"usrname":"Hunter"}}]
	    	    
	    	
	 
	    }catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return objDscore;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return objDscore;
		}
		
		return objDscore;
		
	}
}
